package com.dsp.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.core.util.DspUtil;
import com.core.util.Pagination;
import com.dsp.bean.Jms;
import com.dsp.bean.JobsMessage;
import com.dsp.services.JmsService;
import com.dsp.services.JobsMessageService;

@Controller("JobsMessageController")
public class JobsMessageController extends DspController<JobsMessage>{

	public JobsMessageController() {
		super(JobsMessage.class);
	}

	Logger logger = Logger.getLogger(DspUtil.Log4J);
	
	@Autowired
	private JobsMessageService jobsMessageService;
	
	@Autowired
	private JmsService jmsService; 
	
	@RequestMapping(value = "/jobs_message" , method = RequestMethod.POST)
	@ResponseBody
	public Pagination<JobsMessage> list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.initHttp(request, response);
		DetachedCriteria dc = DetachedCriteria.forClass(JobsMessage.class);
		Long jmsId = Long.valueOf(request.getParameter("jobs"));
		Jms jms = this.jmsService.findById(jmsId);
		jms.setStatus(1L);
		this.jmsService.update(jms);
		Long parent = Long.valueOf((String)request.getParameter("id"));
		dc.add(Restrictions.eq("parent", parent));
		Pagination<JobsMessage> list = this.jobsMessageService.search(dc, (page - 1) * rp, rp);
		list.setPage(page);
		return list;
	}
}
